function setup_edit_sec_group() {

	$('#sec_group_save_button').bind('click', function() {
	
		var aGroupApps = [];
		var aGroupAppsDel = [];
		var i = 0;
		var iDel = 0;
		
		$('.app_available').each(
			function() {
				if ($(this).hasClass('app_not_available') === false) {
					aGroupApps[i] = $(this).attr('id').replace('app_id_', '');
					i++;
				} else {
					aGroupAppsDel[iDel] = $(this).attr('id').replace('app_id_', '');
					iDel++;
				}
			}
		);
	
		var oParams = {
			iGroupId : iGroupId,
			sGroupName : $('#group_name').val(),
			sGroupPublish : (($('#group_publish:checked').length === 1) ? 'Y' : 'N'),
			sGroupApps : aGroupApps.join(','),
			sGroupAppsDel : aGroupAppsDel.join(',')
		};
		
		
		$.post('/admin/user_control/ajax/save_sec_group', oParams, function(data) {
			if (data.sResp) {
				$.prompt('<?php echo $aMessages['admin_sec_group_saved']; ?>');			
			}
		}, 'json');
	});

	$('.app_availability').bind('click', function() {
		$(this).parent().toggleClass('app_not_available');
	});
}



function setup_list_users() {
	$('.user_has_count').bind('click', function() {
		var sLetter = $(this).attr('id').replace('user_lname_', '');
		
		$.post('/admin/user_control/ajax/list_users', { sLetter : sLetter }, function(data) {
			$('#list_users').html(data.sContent);
		}, 'json');
	});
}



function setup_edit_user() {
	
	$('#edit_user_save_button').bind('click', function() {
		var oParams = {
			iUserId : $('#user_id').val(),
			sUserFirstName : $('#user_fname').val(),
			sUserLastName : $('#user_lname').val(),
			sUserEmail : $('#user_email').val(),
			sUserPasswd : $('#user_passwd').val(),
			sUserPasswdChk : $('#user_passwd_chk').val(),
			iGroupId : $('#group_id').val()
		};
		
		
		if (oParams.sUserFirstName === '') {
			$.prompt('Please enter a first name');
			return;
		}
		
		if (oParams.sUserLastName === '') {
			$.prompt('Please enter a last name');
			return;
		}
		
		if (oParams.sUserEmail === '') {
			$.prompt('Please enter an email address');
			return;
		}
		
		if (oParams.iUserId === '0') {
			if (oParams.sUserPasswd === '') {
				$.prompt('Please enter a password');
				return;
			} else if (oParams.sUserPasswd !== oParams.sUserPasswdChk) {
				$.prompt('Your passwords do NOT match!');
				return;
			}
		} else if (oParams.sUserPasswd !== '') {
			if (oParams.sUserPasswd !== oParams.sUserPasswdChk) {
				$.prompt('Your passwords do NOT match!');
				return;
			}
		}
		
		
		$.post('/admin/user_control/ajax/save_edit_user', oParams, function(data) {
			$.prompt(data.sContent);
		}, 'json');
			
		
		
	});
}

$(document).ready(
	function() {
		if ($('.app_availability').length > 0) {
			setup_edit_sec_group();	
		}
		
		
		if ($('.user_has_count').length > 0) {
			setup_list_users();
		}
		
		
		if ($('.user_count_zero').length > 0) {
			$('.user_count_zero').fadeTo(500, 0.5);
		}
		
		
		if ($('#edit_user_save_button').length > 0) {
			setup_edit_user();
		}
	}

);